#include "input_screen_dev.h"

Input_dev Input_screen_dev = {
	.name = "input_screen_dev" ,
	.bind_dir = screen_bind_dir ,
	.init_dev = screen_init_dev ,
	.exit_dev = screen_exit_dev ,
	.flash_input_data = screen_flash_input_data,
	.get_input_data = screen_get_input_data,
};



static int screen_bind_dir(char* name) {
	Input_screen_dev.dir_name = name;
	return 0;
}
static int screen_init_dev() {

	Input_screen_dev.idata = (pInput_data)malloc(sizeof(Input_data)); //设备初始化一定要给对应的指针分配内存
	Input_screen_dev.idata->ts = ts_setup(NULL, 0);
	if(Input_screen_dev.idata->ts == NULL) {
		printf("打开 触摸屏失败\n");
	 	return -1;
	}
	return 0;
}

static int screen_exit_dev() {
	ts_close(Input_screen_dev.idata->ts);
	return 0;
}

static void screen_flash_input_data() {
	
	struct ts_sample samp;
	if(ts_read(Input_screen_dev.idata->ts, &samp,1) != 1) {
		return;
	}
	Input_screen_dev.idata->INPUT_TYPE = INPUT_TYPE_TOUCH;
	Input_screen_dev.idata->ix = samp.x;
	Input_screen_dev.idata->iy = samp.y;
	Input_screen_dev.idata->ipressure = samp.pressure;
	Input_screen_dev.idata->tv = samp.tv;
}

static pInput_data screen_get_input_data(void) {
	screen_flash_input_data();
	return Input_screen_dev.idata;
}


int add_to_input_list(pInput_dev        pcurrent_dev) {
	if(head_input_dev == NULL){
		head_input_dev = (pInput_dev)malloc(sizeof(Input_dev));
	}
	pInput_dev buf_dev = head_input_dev;
	while(buf_dev->next != NULL) {
		buf_dev = buf_dev->next;
	}
	buf_dev->next = pcurrent_dev;
	return 0;
}



